<!--
 * @Author: CtrlC
 * @Date: 2024-08-18 11:10:30
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-08-18 11:10:30
 * @Description: 单词弹窗组件
 * @FilePath: /tech/src/components/word-popup/word-popup.vue
-->
<template>
  <view class="word-popup" :class="{ 'show': visible }">
    <view class="popup-mask" @tap="hide"></view>
    <view class="popup-content">
      <view class="word-header">
        <view class="word-title">{{ wordData.word }}</view>
        <view class="word-phonetic">{{ wordData.phonetic }}</view>
      </view>
      
      <view class="word-definition">{{ wordData.definition }}</view>
      
      <view class="audio-control" @tap="playWordAudio">
        <text class="iconfont icon-sound"></text>
        <text>发音</text>
      </view>
      
      <view class="close-btn" @tap="hide">关闭</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      wordData: {
        word: '',
        phonetic: '',
        definition: '',
        audio: ''
      },
      audioContext: null
    }
  },
  created() {
    // 创建音频上下文
    this.audioContext = uni.createInnerAudioContext();
  },
  beforeDestroy() {
    // 组件销毁前释放资源
    if (this.audioContext) {
      this.audioContext.destroy();
    }
  },
  methods: {
    // 显示弹窗
    show(wordData) {
      if (!wordData) return;
      
      this.wordData = wordData;
      this.visible = true;
    },
    
    // 隐藏弹窗
    hide() {
      this.visible = false;
    },
    
    // 播放单词音频
    playWordAudio() {
      const audioSrc = this.wordData.audio;
      if (!audioSrc) {
        uni.showToast({
          title: '音频资源不存在',
          icon: 'none'
        });
        return;
      }
      
      this.audioContext.src = audioSrc;
      this.audioContext.play();
    }
  }
}
</script>

<style>
.word-popup {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 999;
  display: flex;
  justify-content: center;
  align-items: center;
  visibility: hidden;
  opacity: 0;
  transition: all 0.3s;
}

.word-popup.show {
  visibility: visible;
  opacity: 1;
}

.popup-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
}

.popup-content {
  position: relative;
  width: 80%;
  background-color: white;
  border-radius: 15rpx;
  padding: 40rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.2);
  z-index: 1000;
}

.word-header {
  text-align: center;
  margin-bottom: 30rpx;
}

.word-title {
  font-size: 40rpx;
  font-weight: bold;
  color: #3E7BFA;
  margin-bottom: 10rpx;
}

.word-phonetic {
  font-size: 28rpx;
  color: #666;
}

.word-definition {
  font-size: 32rpx;
  line-height: 1.6;
  padding: 20rpx 0;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
  margin-bottom: 30rpx;
}

.audio-control {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 15rpx 0;
  color: #3E7BFA;
  font-size: 28rpx;
}

.audio-control .iconfont {
  margin-right: 10rpx;
  font-size: 36rpx;
}

.close-btn {
  margin-top: 30rpx;
  text-align: center;
  padding: 15rpx 0;
  background-color: #f0f0f0;
  border-radius: 10rpx;
  color: #666;
  font-size: 28rpx;
}
</style>